//
//  CustomerIntrinsicView.swift
//  Intrinsic Content Size
//
//  Created by NMXY on 2020/12/30.
//

import UIKit

class CustomerIntrinsicView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.cyan
        self.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(label)
        
        NSLayoutConstraint.activate([
            label.leftAnchor.constraint(equalTo: self.leftAnchor),
            label.rightAnchor.constraint(equalTo: self.rightAnchor),
            label.topAnchor.constraint(equalTo: self.topAnchor),
        ])
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //此处加判断如果相等就不更新size
        if  self.bounds.size != intrinsicContentSize{
            invalidateIntrinsicContentSize()
        }
    }
    
    override var intrinsicContentSize: CGSize{
        return self.label.intrinsicContentSize
    }
    
    private lazy var label: UILabel = {
        let tempLabel = UILabel()
        tempLabel.text = "Intrinsic Content Size,来计算Layout布局"
        tempLabel.translatesAutoresizingMaskIntoConstraints = false
        return tempLabel
    }()

    
    
}
